import { Rect, screen, Size, sys, Vec2 } from 'cc';

export enum Platform {
    Unknown = 'unknown',
    Browser = 'browser',
    Wechat = 'wechat',
    Douyin = 'douyin'
}

export type PlatInfo = {
    platform: Platform;
    os: string;
    windowSize: Size;
    networkType: number;
    language: string;
    resolution: Size;
};

export class System {
    static #instance: System = null;
    private constructor() {}
    public static get instance(): System {
        if (!System.#instance) {
            System.#instance = new System();
            System.#instance._getPlatform();
        }
        return System.#instance;
    }

    private platform: PlatInfo;
    private _getPlatform() {
        let plt = Platform.Unknown;
        switch (sys.platform) {
            case sys.Platform.WECHAT_GAME:
                plt = Platform.Wechat;
                break;
            case sys.Platform.MOBILE_BROWSER:
            case sys.Platform.DESKTOP_BROWSER:
                plt = Platform.Browser;
                break;
            case sys.Platform.BYTEDANCE_MINI_GAME:
                plt = Platform.Douyin;
                break;
        }
        this.platform = {
            platform: plt,
            os: sys.os,
            windowSize: screen.windowSize,
            networkType: sys.getNetworkType(),
            language: sys.languageCode,
            resolution: screen.resolution
        };
    }
    public getSafeAreaRect(): Rect {
        return sys.getSafeAreaRect();
    }
    public now(): number {
        return sys.now();
    }
    public getPlatform(): Readonly<PlatInfo> {
        return this.platform;
    }
    public fullScreen() {
        screen.requestFullScreen();
    }
    public isEnterScreen(pos: Vec2, size: Size): boolean {
        const width = this.platform.windowSize.width;
        const height = this.platform.windowSize.height;
        return (
            pos.x + size.width / 2 > -width / 2 &&
            pos.x - size.width / 2 < width / 2 &&
            pos.y + size.height / 2 > -height / 2 &&
            pos.y - size.height / 2 < height / 2
        );
    }
}
